const db = require("../../config/db");

module.exports = {
    edit_address(address_id, address_name, address_phone, address_province, address_city, address_county, address_detail) {
        return new Promise((resolve, reject) => {
            let check_address__sql = `select address_id from db_address where address_id="${address_id}"`;
            db.query(check_address__sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    if (rows.length == 0) {
                        let add_address_sql = `insert into db_address(address_id,address_name,address_phone, address_province, address_city, address_county,address_detail) values("${address_id}","${address_name}","${address_phone}","${address_province}","${address_city}","${address_county}","${address_detail}")`;
                        db.query(add_address_sql, (error, result) => {
                            if (error) {
                                reject(error);
                            } else {
                                console.log("3.0", result);
                                resolve(result);
                            }
                        });
                    } else {
                        // update stuinfo set sname='码仙4',saddress='木星',sclass=4,ssex='女' where sid=1;
                        let set_address_sql = `update db_address set address_name="${address_name}",address_phone=${address_phone},address_province="${address_province}",address_city="${address_city}",address_county="${address_county}",address_detail="${address_detail}" where address_id="${address_id}"`;
                        db.query(set_address_sql, (e, r) => {
                            if (e) {
                                reject(e);
                            } else {
                                resolve();
                            }
                        });
                    }
                }
            });
        });
    },

    remove_addres(address_id) {
        return new Promise((resolve, reject) => {
            let remove_address_sql = `DELETE FROM db_address WHERE address_id="${address_id}"`;
            db.query(remove_address_sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    resolve();
                }
            });
        });
    },

    get_address() {
        return new Promise((resolve, reject) => {
            let get_address_sql = `select * from db_address`;
            db.query(get_address_sql, (err, result) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "获取地址失败",
                        err,
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "地址获取成功",
                        result,
                    });
                }
            });
        });
    },

    get_addr(address_id) {
        return new Promise((resolve, reject) => {
            let get_addr_sql = `select * from db_address where address_id="${address_id}"`;
            db.query(get_addr_sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(rows);
                }
            });
        });
    },
};
